package com.haisen.service.impl;

import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.dto.ItemDto;
import com.haisen.dataobject.dto.UserDto;
import com.haisen.dataobject.entity.Area;
import com.haisen.dataobject.entity.Item;
import com.haisen.dataobject.entity.ItemParamValue;
import com.haisen.dataobject.entity.User;
import com.haisen.dataobject.mapper.base.AreaMapper;
import com.haisen.dataobject.mapper.base.ItemMapper;
import com.haisen.dataobject.query.ItemQuery;
import com.haisen.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService{

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 插入操作，如果有页面传递参数不够，逻辑在这里处理
     * @param item
     */
    @Override
    public void createItem(Item item) {
        itemMapper.insertSelective(item);
    }

    @Override
    public void updateItem(Item item) {
          itemMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public void deleteItem(Long itemId) {
        itemMapper.deleteByPrimaryKey(itemId);

    }

    @Override
    public Item findOneById(Long itemId) {
        return itemMapper.selectByPrimaryKey(itemId);
    }

    @Override
    public List<Item> find(Weekend example) {
        return itemMapper.selectByExample(example);
    }

    @Override
    public List<Item> findAll() {
        return itemMapper.selectAll();
    }

    @Override
    public PageResultSet<ItemDto> findByPage(ItemQuery itemQuery) {

        if(!StringUtils.isEmpty(itemQuery.getOrderBy())) {
            PageHelper.orderBy(itemQuery.getOrderBy());
        }

       /* Weekend<Item> example = Weekend.of(Item.class);
        WeekendCriteria<Item, Object> criteria = example.weekendCriteria();

        if (!StringUtils.isEmpty(itemQuery.getAreaId())) {
            criteria.andEqualTo(Item::getAreaId, "%" + itemQuery.getAreaId() + "%");
        }
        if(!StringUtils.isEmpty(itemQuery.getTitle())) {
            criteria.andLike(Item::getTitle,itemQuery.getTitle());
        }

        if(!StringUtils.isEmpty(itemQuery.getSellPoint())) {
            criteria.andLike(Item::getSellPoint,itemQuery.getSellPoint());
        }*/

        //页面分页的结果
        PageHelper.offsetPage(itemQuery.getOffset(), itemQuery.getLimit());
        //有不同条件的，但是自定义mapp中没有设置条件，如何处理？，要么发多条sql语句让后拼接
       //List<Item> items = itemMapper.selectByExample(example);
        //long total = itemMapper.selectCountByExample(example);
        List<ItemDto> itemDtos  = itemMapper.findDtoByWhere(itemQuery);
        //将itemdto的地市名称封装下;
         List<Area> areaList = areaMapper.selectAll();
      List<ItemDto> itemDtosnew =   itemDtos.stream().map( o -> {
              for (Area area : areaList){
                  if (area.getId() == o.getCountryId()){
                      o.setCountryName(area.getName());
                      //再循环着地市
                       o.setCityId(area.getParentId());
                      return o;
                  }
              }
              return o;
         }).map( o ->{
          for (Area area : areaList){
              if (area.getId() == o.getCityId()){
                  o.setCityName(area.getName());
                  return o;
              }
          }
          return o;
      }).collect(Collectors.toList());


         long total = itemMapper.selectDtoCountByWhere(itemQuery);
        PageResultSet<ItemDto> resultSet = new PageResultSet<>();
        resultSet.setRows(itemDtosnew);
        resultSet.setTotal(total);
        return resultSet;
    }

    @Override
    public Long findMaxId() {
        //todo:懒加载，需要找出最后一条记录的id
        return  itemMapper.selectMaxId();
    }
}
